﻿using System;
using System.ComponentModel;
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;

namespace Caraya.MFG.BusinessPlugIn
{
    /// <summary>
    /// 其他出库单分录选销售订单分录插件
    /// </summary>
    [Description("其他出库单选销售订单插件"), HotUpdate]
    public class MisDeliveryEdit : AbstractBillPlugIn
    {
        private DynamicObject LoadBDFullObject(string formId, object number)
        {
            var ctx = this.Context;
            FormMetadata meta = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
            var queryParam = new QueryBuilderParemeter
            {
                FormId = formId,
                BusinessInfo = meta.BusinessInfo,
                FilterClauseWihtKey = string.Format(" {0} = '{1}' ", meta.BusinessInfo.GetForm().MasterPKFieldName, number)
            };
            var bdObjs = BusinessDataServiceHelper.Load(ctx, meta.BusinessInfo.GetDynamicObjectType(), queryParam);
            return bdObjs[0];
        }
        public override void BeforeF7Select(BeforeF7SelectEventArgs e)
        {
            base.BeforeF7Select(e);
            string entityKey = "FProName";
            if (e.FieldKey.EqualsIgnoreCase(entityKey))
            {
                ListShowParameter showParam = new ListShowParameter()
                {
                    Context = this.Context,
                    FormId = "SAL_SaleOrder",
                    IsShowApproved = true,
                    IsShowQuickFilter = true,
                    ParentPageId = this.View.PageId,
                    IsLookUp = true,
                    MultiSelect = true,
                    PermissionItemId = Kingdee.BOS.Core.Permission.PermissionConst.View
                };
                // string filter = string.Format(@"FMaterialId.FNumber='{0}'", "30001");
                // showParam.ListFilterParameter.Filter = filter;
                showParam.OpenStyle.ShowType = ShowType.Modal;
                this.View.ShowForm(showParam, new Action<FormResult>((results) =>
                {
                    if (results.ReturnData is ListSelectedRowCollection data)
                    {
                        var ap = this.View.LayoutInfo.GetFieldAppearance(entityKey);
                        var rowCount = this.Model.GetEntryRowCount(ap.EntityKey);
                        var maxRowIndex = e.Row + data.Count;
                        if (maxRowIndex > rowCount)
                        {
                            this.Model.BatchCreateNewEntryRow(ap.EntityKey, maxRowIndex - rowCount);
                        }

                        for (int i = 0; i < data.Count; i++)
                        {
                            var mid = data[i].DataRow["FMaterialId_Id"];
                            var material = LoadBDFullObject("BD_MATERIAL", mid);
                            LocaleValue langValue = material["Name"] as LocaleValue;
                            this.View.Model.SetValue(entityKey, langValue[2052], e.Row + i);
                        }
                    }
                }));
            }

        }
    }
}
